/*
 * 文 件    名:  CacheConfigModel.java
 * 创建日期:  2013-3-18下午6:41:19
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2013,  All rights reserved
 * 作          者:  xrj66010
 */
package com.weipu.common.facade.model.config;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.weipu.common.constants.ServerConstant;

/** 
 * 用户配置文件
 * @version [版本号, 2013-3-18]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author xrj66010
 */
public class CacheConfigModel implements Serializable
{
    /** 
     * @Fields serialVersionUID : TODO（用一句话描述这个变量表示什么）
     */
    private static final long serialVersionUID = 1830988218152915907L;
    
    private boolean enableMemoryCache;//内存缓存开关
    
    private double preLoadRate; //预加载比率(相对超时时间)
    
    private boolean enableLocalDBCache;//是否允许本地数据库缓存
    
    private boolean enableLog;//是否允许打印log
    
    private boolean enablePreLoad;//是否允许预加载，异步数据缓存刷新会使用到
    
    public boolean isEnablePreLoad()
    {
        return enablePreLoad;
    }
    
    private String entityID;
    
    private List<CacheConfig> cacheConfigList;
    
    public double getPreLoadRate()
    {
        return preLoadRate;
    }
    
    public boolean isEnableLocalDBCache()
    {
        return enableLocalDBCache;
    }
    
    public boolean isEnableLog()
    {
        return enableLog;
    }
    
    public String getEntityID()
    {
        return entityID;
    }
    
    public void setEntityID(String entityID)
    {
        this.entityID = entityID;
    }
    
    public List<CacheConfig> getCacheConfigList()
    {
        return cacheConfigList;
    }
    
    public void setCacheConfigList(List<CacheConfig> cacheConfigList)
    {
        this.cacheConfigList = cacheConfigList;
    }
    
    public boolean isEnableCache()
    {
        return enableMemoryCache;
    }
    
    public void setEnableCache(boolean enableCache)
    {
        this.enableMemoryCache = enableCache;
    }
    
    /**
      * Json转Model
      * @param json
      * @return  [参数说明]
      * @return CacheConfigModel [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
      */
    public static CacheConfigModel json2Cache(JSONObject json)
    {
        if (null == json)
        {
            return null;
        }
        CacheConfigModel cacheConfigModel = null;
        try
        {
            cacheConfigModel = new CacheConfigModel();
            
            cacheConfigModel.setEnableCache(json.optBoolean(ServerConstant.User.CONSTANT_ENABLECACHE, true));//默认开启内存缓存
            cacheConfigModel.preLoadRate = json.optDouble(ServerConstant.User.CONSTANT_PRE_LOAD_RATE, 0.25F);//默认比例为25%
            cacheConfigModel.enableLocalDBCache =
                json.optBoolean(ServerConstant.User.CONSTANT_ENABLE_LOCAL_DB_CACHE, true);//默认支持本地数据库缓存
            cacheConfigModel.enableLog = json.optBoolean(ServerConstant.User.CONSTANT_ENABLE_LOG, true);//日志默认打开
            cacheConfigModel.enablePreLoad = json.optBoolean(ServerConstant.User.CONSTANT_ENABLE_PRE_LOAD, true);//默认预加载
            
            if (json.optString(ServerConstant.User.CONSTANT_ENTITYID) != JSONObject.NULL
                && !json.isNull(ServerConstant.User.CONSTANT_ENTITYID))
            {
                cacheConfigModel.setEntityID(json.optString(ServerConstant.User.CONSTANT_ENTITYID));
            }
            
            List<CacheConfig> list = new ArrayList<CacheConfig>();
            JSONArray jonsCacheConfig = json.optJSONArray(ServerConstant.User.CONSTANT_CACHECONFIGLIST);
            for (int i = 0; i < jonsCacheConfig.length(); i++)
            {
                CacheConfig cacheConfig = new CacheConfig();
                if (jonsCacheConfig.getJSONObject(i).optString(ServerConstant.User.CONSTANT_TYPE) != JSONObject.NULL
                    && !jonsCacheConfig.getJSONObject(i).isNull(ServerConstant.User.CONSTANT_TYPE))
                {
                    cacheConfig.setType(jonsCacheConfig.getJSONObject(i).optString(ServerConstant.User.CONSTANT_TYPE));
                }
                cacheConfig.setEnable(jonsCacheConfig.getJSONObject(i).optBoolean(ServerConstant.User.CONSTANT_ENABLE));
                cacheConfig.setExpireTime(jonsCacheConfig.getJSONObject(i)
                    .optInt(ServerConstant.User.CONSTANT_EXPIRETIME));
                
                if (jonsCacheConfig.getJSONObject(i).optString(ServerConstant.User.CONSTANT_ENTITYID) != JSONObject.NULL
                    && !jonsCacheConfig.getJSONObject(i).isNull(ServerConstant.User.CONSTANT_ENTITYID))
                {
                    cacheConfig.setEntityID(jonsCacheConfig.getJSONObject(i)
                        .optString(ServerConstant.User.CONSTANT_ENTITYID));
                }
                
                cacheConfig.setEnableDB(jonsCacheConfig.getJSONObject(i)
                    .optBoolean(ServerConstant.User.CONSTANT_ENABLE_DATA_BASE, true));
                list.add(cacheConfig);
            }
            cacheConfigModel.setCacheConfigList(list);
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return cacheConfigModel;
    }
    
    public static CacheConfigModel json2Cache(String jsonString)
    {
        if (TextUtils.isEmpty(jsonString))
        {
            return null;
        }
        CacheConfigModel cacheConfigModel = null;
        try
        {
            JSONObject json = new JSONObject(jsonString);
            cacheConfigModel = CacheConfigModel.json2Cache(json);
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        
        return cacheConfigModel;
    }
    
    @Override
    public int hashCode()
    {
        return toString().hashCode();
    }
    
    @Override
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        
        sb.append("enableMemoryCache: ").append(enableMemoryCache).append('\n');
        sb.append("preLoadRate: ").append(preLoadRate).append('\n');
        sb.append("enableLocalDBCache: ").append(enableLocalDBCache).append('\n');
        sb.append("enableLog: ").append(enableLog).append('\n');
        sb.append("enablePreLoad: ").append(enablePreLoad).append('\n');
        sb.append("cacheConfigList: [").append(cacheConfigList).append(']');
        return sb.toString();
        
    }
    
}
